<template>
	<view>
		<van-sticky offset-top="44">
			<ul class="top">
				<li v-for="(item,index) in itemList" :key="index" :class="activeClass == index ? 'active' : ''"
					@click="changeClass(index)">
					{{item}}
				</li>
			</ul>
		</van-sticky>
		<view class="info" v-show="activeClass==0">
			<view class="empty-item" v-if="!donateList.length">
				<u-empty mode="data" text="暂无数据"></u-empty>
			</view>
			<view v-else>
				<view class="item" v-for="item in donateList" :key="item.goodsId" @click="DonateDetail(item)">
					<image :src="item.picture">
					</image>
					<view class="right">
						<span class="title">
							{{item.title}}
						</span>
						<span class="author" v-if="item.author">
							新旧程度: {{item.author}}
						</span>
						<span class="message">
							{{item.goodsDesc}}
						</span>
					</view>
				</view>
				<uni-load-more :status="status1" :contentText="contentText"></uni-load-more>
			</view>
		</view>
		<view class="info" v-show="activeClass==1">
			<view class="empty-item" v-if="!donateList.length">
				<u-empty mode="data" text="暂无数据"></u-empty>
			</view>
			<view class="item" v-for="item in needList" :key="item.goodsId" @click="NeedDetail(item)">
				<image :src="item.picture" mode="">
				</image>
				<view class="right">
					<span class="title">
						{{item.title}}
					</span>
					<span class="author" v-if="item.author">
						新旧程度: {{item.author}}
					</span>
					<span class="message">
						{{item.goodsDesc}}
					</span>
				</view>
			</view>
			<uni-load-more :status="status2" :contentText="contentText"></uni-load-more>
		</view>
	</view>
</template>

<script>
	export default {
		name: "Info",
		data() {
			return {
				activeClass: 0,
				itemList: [
					'捐赠信息', '需求信息'
				],
				donatePage: 1, //捐赠信息列表页码
				needPage: 1, //需求信息列表页码
				donateList: [],
				needList: [],
				contentText: {
					contentdown: '上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
				status1: 'loading',
				status2: 'loading',
				isBottomRefresh1: false,
				isBottomRefresh2: false,
			};
		},
		mounted() {
			this.getDonateList(),
				this.getNeedList()
		},
		methods: {
			//切换捐赠信息列表和需求信息列表
			changeClass(index) {
				this.activeClass = index
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 200
				})
			},
			// 添加到历史
			addHistory(info) {
				this.$myRequest({
					url: '/CollHis/addHis',
					method: 'post',
					data: {
						userId: uni.getStorageSync('userId'),
						goodsId: info.goodsId
					}
				})
			},
			// 跳转到捐赠详情页
			DonateDetail(info) {
				console.log(info);
				uni.navigateTo({
					url: '/page_index/DonateDetail?info=' + encodeURIComponent(JSON.stringify(info))
				})
				this.addHistory(info)
			},
			// 跳转到需求详情页
			NeedDetail(info) {
				uni.navigateTo({
					url: '/page_index/NeedDetail?info=' + encodeURIComponent(JSON.stringify(info))
				})
				this.addHistory(info)
			},
			// 获取捐赠信息列表
			async getDonateList() {
				var that = this;
				var {
					data: res
				} = await this.$myRequest({
					url: '/goodsBefore/getDonaGoods/' + that.donatePage + '/5',
				})
				if (res.code == 20000) {
					if (this.isBottomRefresh1) { //判断是第一次获取捐赠信息还是触底加载新数据
						this.donateList = this.donateList.concat(res.data.list)
					} else {
						this.donateList = res.data.list
					}
					this.ifBottomRefresh1 = false
					console.log(this.donateList);
				}
				this.status1 = this.donateList.length < res.data.total ? 'loading' : 'noMore'
			},
			// 获取需求信息列表
			async getNeedList() {
				var that = this;
				var {
					data: res
				} = await this.$myRequest({
					url: '/goodsBefore/getAccGoods/' + that.needPage + '/5',
				})
				if (res.code == 20000) {
					if (this.isBottomRefresh2) { //判断是第一次获取需求信息还是触底加载新数据
						this.needList = this.needList.concat(res.data.list)
					} else {
						this.needList = res.data.list
					}
					// this.ifBottomRefresh2 = false
					console.log(this.needList);
				}
				this.status2 = this.needList.length < res.data.total ? 'loading' : 'noMore'
			},

		}
	}
</script>

<style lang="scss" scoped>
	.top {
		padding: 0;
		display: flex;
		margin: 20rpx 20rpx 0rpx;
		height: 80rpx;
		background-color: white;
		border-radius: 20rpx;
		justify-content: space-around;

		li {
			position: relative;
			line-height: 80rpx;
			float: left;
			font-size: 34rpx;
			font-weight: bold;
			color: #8a8a8a;
			list-style: none;
		}

		li.active {
			color: black;

		}

		li.active::after {
			position: absolute;
			left: 10rpx;
			z-index: 10;
			bottom: 0rpx;
			content: '';
			width: 116rpx;
			height: 6rpx;
			border-radius: 30rpx;
			background-color: #d81e06;
		}

	}

	.info {
		.item {
			border-radius: 20rpx;
			background-color: #fff;
			margin: 10rpx 20rpx;
			height: 250rpx;
			width: 710rpx;
			display: flex;

			image {
				width: 160rpx;
				height: 200rpx;
				margin: 20rpx 20rpx;
			}

			.right {
				margin-left: 20rpx;
				width: 65%;

				.title {
					display: block;
					color: #d81e06;
					margin-top: 20rpx;
					font-size: 28rpx;
					font-weight: bold;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.author {
					display: block;
					font-size: 24rpx;
					color: gray;
					margin-top: 20rpx;
				}

				.message {
					font-size: 24rpx;
					margin-top: 20rpx;
					overflow: hidden;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					text-overflow: ellipsis;
				}
			}
		}

		.empty-item {
			border-radius: 20rpx;
			background-color: #fff;
			margin: 0 20rpx 10rpx;
			height: 240rpx;
			width: 710rpx;
		}
	}
</style>
